package com.sevencar.activity;

import me.maxwin.view.SwitchButton;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.lightble.CmdSend;
import com.example.lightble.R;


public class LightDevItem extends LinearLayout{

	private Activity mActivity;
//	private BluetoothDevice mDevice;
    TextView mTvName;
    TextView mTvHint; //提示, 如设备正在绑定,请在弹出的框中输入密码.
    private SwitchButton mBtnOpenButton;
	private Button mConnectButton;
	private ProgressBar mConnectPrograss;
	private boolean mIsOpen; //当前灯是否打开.
	private boolean isConnected;

	private String  _address ;
	private String  _name ;
	
    public LightDevItem(Activity acty,BluetoothDevice device) {
        super(acty);
        mActivity=acty;
//        mDevice=device;
        this._address  = device.getAddress();
    	this._name = device.getName();
        initViews();
    }
    public LightDevItem(Activity acty,String address,String name) {
    	super(acty);
    	mActivity=acty;
    	this._address  = address;
    	this._name = name;
    	initViews();
    }
    private void initViews()
    {
        LayoutInflater inflater=(LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.light_dev_item,this);
        
        mBtnOpenButton=(SwitchButton)findViewById(R.id.id_lighctrl_item_switch);
        mConnectButton=(Button) findViewById(R.id.id_lighctrl_item_cbx);
        mConnectPrograss=(ProgressBar)findViewById(R.id.id_light_dev_process);
        
        mTvName=(TextView)findViewById(R.id.id_light_dev_name);
        mTvHint=(TextView)findViewById(R.id.id_light_dev_hint);
        
        mTvName.setText(_name);
    	mBtnOpenButton.setEnabled(false);
        mConnectPrograss.setVisibility(View.GONE);
     
        mConnectPrograss.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setDisconnect(true);
			}
		});
        mConnectButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
						if(isConnected)
						{
							setDisconnect(true);
						}else{
							mConnectPrograss.setVisibility(View.VISIBLE);
			    			mConnectButton.setVisibility(View.GONE);
			    			HomeFragment.mBLEServiceInstance.startConnectByUser(_address);
						}  
			}
		});
        mBtnOpenButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				    Log.i("sno","mOpenButton.setOnCheckedChangeListener isChecked="+isChecked);
				 	mIsOpen=isChecked;
			    	byte[] test=CmdSend.getInstance().getOpenLightArray(isChecked);
			    	HomeFragment.mBLEServiceInstance.sendDataToAddress(test,_address);
			}
		});
        mIsOpen=false;
    }
    public String getDevAddress()
    {
    	return _address;
    }
    public String getDevName()
    {
    	return _name;
    }
//    public BluetoothDevice getDevice1()
//    {
//    	return mDevice;
//    }
    /**
     * 设备重连失败.
     */
    public void setReconnectFailed()
    {
    	Log.i("sno","重重重重重重重重重重重重重重重重重重重重重重重重重重重重重重重重");
    	mConnectPrograss.setVisibility(View.VISIBLE);
		mConnectButton.setVisibility(View.GONE);
		HomeFragment.mBLEServiceInstance.startConnectByUser(_address);
    }
    public void setConnecting()
    {
    	mConnectPrograss.setVisibility(View.VISIBLE);
		mConnectButton.setVisibility(View.GONE);
    }
    public void setDisconnect(boolean byUser)
    {
    	mConnectPrograss.setVisibility(View.GONE);
		mConnectButton.setVisibility(View.VISIBLE);
		mConnectButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.checkbox_off));
		setHintText("");
		mBtnOpenButton.setChecked(false);
		mBtnOpenButton.setEnabled(false);
		isConnected=false;
		if(byUser){
			HomeFragment.mBLEServiceInstance.setDisconnectByUser(_address);
		}
    }
    public void setConnected(boolean isOpen)
    {
    	mConnectButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.checkbox_on));
    	mConnectPrograss.setVisibility(View.GONE);
		mConnectButton.setVisibility(View.VISIBLE);
		setHintText(_address);
		if(_name!=null)
			mTvName.setText(_name);  //更新名字.
		
		mIsOpen=isOpen;
    	mBtnOpenButton.setEnabled(true);
    	if(mIsOpen){
    		mBtnOpenButton.setChecked(true);
    	}else{
    		mBtnOpenButton.setChecked(false);
    	}
    	isConnected=true;
    }
    //设置提示字符串.
    public void setHintText(String str)
    {
    	if(mTvHint!=null){
    		mTvHint.setText(str);
    	}
    }
    public void showBondPasswdDialog(String strHint)
    {
    	   final EditText input = new EditText(mActivity);
           AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
           builder.setTitle(strHint).setView(input)
                   .setNegativeButton("Cancel", null);
           builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

               public void onClick(DialogInterface dialog, int which) {
            	   String passwd=input.getText().toString();
            	   HomeFragment.mBLEServiceInstance.sendNewPasswdByUser(_address,passwd);
                }
           });
           builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				setDisconnect(true);
			}
		});
       builder.show();
    }
    public void onDestoryDev(){
    	
    }
}

